package com.unittec.drainage.provider.auth.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.unittec.drainage.sdk.auth.request.Req101520;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unittec.drainage.provider.auth.entity.SysAppVersion;
import com.unittec.drainage.provider.auth.mapper.SysAppVersionMapper;
@Service
public class SysAppVersionService extends ServiceImpl<SysAppVersionMapper, SysAppVersion> {

    //根据条件查询versionPage
    public Page<SysAppVersion> selectPageVersion(Req101520 content) {
        LambdaQueryWrapper<SysAppVersion> qw = new LambdaQueryWrapper<>();
        //根据机型
        if(StringUtils.isNotEmpty(content.getModel())){
            qw.eq(SysAppVersion::getModel,content.getModel());
        }
        //根据版本名
        if(StringUtils.isNotEmpty(content.getVersionName())){
            qw.like(SysAppVersion::getVersionName,content.getVersionName());
        }
        //根据size
        if(content.getSize()==null){
            content.setSize(10L);
        }
        //根据当前页
        if(content.getCurrent()==null){
            content.setCurrent(1L);
        }
        //倒序
        qw.orderByDesc(SysAppVersion::getVersionCode);
        Page<SysAppVersion> page = new Page<>(content.getCurrent(), content.getSize());
        this.page(page,qw);
        return page;
    }

    //判断该versionCode是否已经存在
    public boolean judgeVersionCode(Integer versionCode,String model) {
        LambdaQueryWrapper<SysAppVersion> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysAppVersion::getVersionCode,versionCode);
        lambdaQueryWrapper.eq(SysAppVersion::getModel,model);
        if (this.getOne(lambdaQueryWrapper)==null){
            return false;//不存在
        }
        return true;//存在
    }

    //判断versionName是否存在
    public boolean judgeVersionName(String versionName,String model) {
        LambdaQueryWrapper<SysAppVersion> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysAppVersion::getVersionName,versionName);
        lambdaQueryWrapper.eq(SysAppVersion::getModel,model);
        if(null == this.getOne(lambdaQueryWrapper)){
            return false;//不存在
        }
        return true;//存在
    }

    //判断该versionCode是否已经存在,除了自己之外
    public boolean judgeVersionCodeExceptThis(Integer versionCode, String id,String model) {
        LambdaQueryWrapper<SysAppVersion> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysAppVersion::getVersionCode,versionCode);
        lambdaQueryWrapper.ne(SysAppVersion::getId,id);
        lambdaQueryWrapper.eq(SysAppVersion::getModel,model);
        if(null==this.getOne(lambdaQueryWrapper)){
            return false;//不存在
        }
        return true;//存在
    }

    ////判断该versionName是否已经存在,除了自己之外
    public boolean judgeVersionNameExceptThis(String versionName, String id,String model) {
        LambdaQueryWrapper<SysAppVersion> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .ne(SysAppVersion::getId,id)
                .eq(SysAppVersion::getModel,model)
                .eq(SysAppVersion::getVersionName,versionName);
        if(null == this.getOne(lambdaQueryWrapper)){
            return false;//不存在
        }
        return true;//存在
    }

    //根据机型获取最新app版本
    public SysAppVersion getLatestByModel(String model) {
        LambdaQueryWrapper<SysAppVersion> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(SysAppVersion::getModel,model)
                .orderByDesc(SysAppVersion::getVersionCode);
        List<SysAppVersion> list = this.list(lambdaQueryWrapper);
        if(list.size()>0) {
            return list.get(0);
        }
        return null;
    }



}
